
package com.web.app.controller;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.opensymphony.oscache.util.StringUtil;
import com.web.dao.domain.Role;
import com.web.dao.service.RoleService;

/**
 * 
 * @author NhuKhang
 *
 */

@Controller
public class HomeController extends ApplicationObjectSupport {
	
	@Autowired
	private RoleService roleService;
	
	@RequestMapping("/admin/role/list.html")
	public ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		List<Role> roles = roleService.findAll();
		ModelAndView model = new ModelAndView("admin/role/list");
		model.addObject("msg", "hello world!");
		model.addObject("roles", roles);

		return model;
	}
	
	@RequestMapping("/admin/role/edit.html")
	public ModelAndView addRole(@ModelAttribute("role") Role role){
		ModelAndView mav = new ModelAndView("admin/role/edit");
		if(!StringUtil.isEmpty(role.getRole())){
			role.setCreatedDate(new Timestamp(new Date().getTime()));
			roleService.save(role);
			mav = new ModelAndView("redirect:/admin/role/list.html");
		}
		return mav;
	}
}
